package cn.txun.csmall.sentinal.study.controller;

import cn.txun.csmall.sentinal.study.block.GlobalBlockException;
import cn.txun.csmall.sentinal.study.service.HelloService;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.StandardEmitterMBean;

@Slf4j
@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    @SentinelResource(value = "sayHi",
            blockHandler = "sayHiBlockHandle",
            blockHandlerClass = BlockException.class)
    public String sayHi(String name){
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String sayHi = helloService.doSayHi(name);
        return sayHi;
    }



   /* @GetMapping("/hello")
    public String sayHi(String name){
        //准备资源入口
        Entry entry = null;
        String sayHi = null;
        try{
            //开启资源保护的入口 给entry赋值
            entry = SphU.entry("sayHi");
            sayHi = helloService.doSayHi(name);
        }catch (BlockException e){
            //根据我们定义的规则，流控规则，需要对资源调用
            //进行流控异常的捕获
            //异常捕获时，表示当前资源访问以及违反，达到规则的临界点
            sayHi = "达到限流规则阈值,blocked";
        }finally {
            //释放资源
            if(entry!=null){
                entry.exit();
            }
        }
        return sayHi;
    }*/
}
